/*
 * @lc app=leetcode id=481 lang=cpp
 *
 * [481] Magical String
 */

// @lc code=start
class Solution {
public:
    int magicalString(int n) {
    string s = "122";
        int i = 2;
        while (i < n)
        {
            s += string(s[i++] - '0', s.back() == '1' ? '2': '1');
        }

        return count(s.begin(),s.begin()+n,'1');
        
    }
};
// @lc code=end

